package com.ts.quartz.daoimpl;

import com.ts.comm.SysResult;
import com.ts.jdbc.SysDB;
import com.ts.quartz.bean.ScheduleInfo;
import com.ts.quartz.dao.ScheduleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * Created by yingzi on 2017-6-22.
 */
@Repository
public class ScheduleDaoImpl implements ScheduleDao
{

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    /**
     * 查询所有Job列表
     * @return
     */
    public SysResult queryJobs() {
        String sql="select * from s_task order by 1";
        SysResult sysResult=new SysResult();
        try {
            sysResult.setRows(SysDB.getRows(jdbcTemplate,sql));
        } catch (Exception e) {
           sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }

    public SysResult getJob(int iFID) {
        String sql="select * from s_task where fid=:id order by 1";
        SysResult sysResult=new SysResult();
        try {
            HashMap<String,Object> parm=new HashMap<String, Object>();
            parm.put("id",iFID);
            sysResult.setMaprow(SysDB.getMapValue(jdbcTemplate,sql.toString(),parm));
        } catch (Exception e) {
            sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }

    /**
     * 添加一个Job
     * @param job
     * @return
     */
    @Transactional
    public SysResult addJob(ScheduleInfo job) {
        SysResult sysResult=new SysResult();
        try {
            StringBuilder sql=new StringBuilder("");
            sql.append("select nvl(max(fid),0)+1 from s_task ");
            int iMaxID=SysDB.getIntValue(jdbcTemplate,sql.toString());
            sql=new StringBuilder("");
            sql.append("insert into s_task ");
            sql.append(" select :id,:beanname,:methodname,:params,:cron,:status,:remark,:createuser,:createtime from dual");
            HashMap<String,Object> param=new HashMap<String, Object>();
            param.put("id",iMaxID);
            param.put("beanname",job.getBeanName());
            param.put("methodname",job.getMethodName());
            param.put("params",job.getParams());
            param.put("cron",job.getCronExpr());
            param.put("status",job.getStatus());
            param.put("remark",job.getRemark());
            param.put("createuser",job.getCreateUser());
            param.put("createtime",job.getCreateTime());

            SysDB.update(jdbcTemplate,sql.toString(),param);
        } catch (Exception e) {
            e.printStackTrace();
            sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }

    /**
     * 更新一个Job
     * @param job
     * @param jobId
     * @return
     */
    public SysResult updateJob(ScheduleInfo job, int jobId) {
        SysResult sysResult=new SysResult();
        try {
            StringBuilder sql=new StringBuilder("");
            sql.append("update s_task set ");
            sql.append(" fBeanName=:beanname,fMethodName=:methodname,fParams=:params,fCron=:cron");
            sql.append(",fStatus=:status,fRemark=:remark");
            sql.append(" where fId=:id");
            HashMap<String,Object> param=new HashMap<String, Object>();
            param.put("beanname",job.getBeanName());
            param.put("methodname",job.getMethodName());
            param.put("params",job.getParams());
            param.put("cron",job.getCronExpr());
            param.put("remark",job.getRemark());
            param.put("id",jobId);

            SysDB.update(jdbcTemplate,sql.toString(),param);
        } catch (Exception e) {
            sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }

    /**
     * 删除Job
     * @param jobId
     * @return
     */
    public SysResult deleteJob(int jobId) {
        SysResult sysResult=new SysResult();
        try {
            StringBuilder sql=new StringBuilder("");
            sql.append("delete from s_task  ");
            sql.append(" where fId=:id");
            HashMap<String,Object> param=new HashMap<String, Object>();
            param.put("id",jobId);

            SysDB.update(jdbcTemplate,sql.toString(),param);
        } catch (Exception e) {
            sysResult.onSetErrorMsg(e);
        }
        return sysResult;
    }

    /**
     * 暂停
     * @param jobId
     * @return
     */
    public SysResult pauseJob(int jobId) {
        return null;
    }

    /**
     * 恢复
     * @param jobId
     * @return
     */
    public SysResult resumeJob(int jobId) {
        return null;
    }

    /**
     * 启动
     * @param jobId
     * @return
     */
    public SysResult runJob(int jobId) {
        return null;
    }
}
